﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using InHD.dataaccess;
using System.Windows.Input;
using System.Windows.Controls;
using InHD.commands;
using System.Windows.Documents;
using System.IO;
using System.Windows.Markup;
using System.Windows.Xps.Packaging;
using System.IO.Packaging;
using System.Windows.Xps.Serialization;
using System.Windows;
using InHD.utilities;
using InHD.Properties;

namespace InHD.viewmodels
{
    class RegisterPrintingViewModel: WorkspaceViewModel
    {
        #region Value Property
        readonly InHD.model.RegisterPrinting _registerPrinting;
        readonly RegisterPrintingRepository _registerPrintingRepository;
        readonly CompanyInfo _companyInfo;
        private RegisterInfo _registerInfo = new RegisterInfo();
        private WorkspaceViewModel _pageContent;

        #endregion

        #region Presentation Property
        public WorkspaceViewModel PageContent
        {
            get
            {
                return _pageContent;
            }
            set
            {
                if (_pageContent != value)
                {
                    _pageContent = value;
                    this.OnPropertyChanged("PageContent");
                }
            }

        }
        #endregion

        #region Constructor

        public RegisterPrintingViewModel(RegisterPrinting registerPrinting, RegisterPrintingRepository registerPrintingRepository, CompanyInfo companyInfo)
        {
            if (registerPrinting == null)
                throw new ArgumentNullException("registerPrinting");
            if (registerPrintingRepository == null)
                throw new ArgumentNullException("registerPrintingRepository");
            if (companyInfo == null)
                throw new ArgumentNullException("companyInfo");

            this._registerPrinting = registerPrinting;
            this._registerPrintingRepository = registerPrintingRepository;
            this._companyInfo = companyInfo;
            _pageContent = new RegisterStartPage(_companyInfo, _registerInfo);
            _pageContent.RequestClose += this.HandlePageClose;
        }

        #endregion

        #region Normal Method

        public void HandlePageClose(object source, EventArgs e)
        {
            if (this.PageContent is RegisterStep1)
            {
                if (((RegisterStep1)PageContent).commandExcuted.Equals("Next"))
                {
                    string regResult = ((RegisterStep1)PageContent).RegResult;
                    this.PageContent.RequestClose -= this.HandlePageClose;
                    this.PageContent = new RegisterStep2(regResult);
                    this.PageContent.RequestClose += this.HandlePageClose;
                    OnPropertyChanged("PageContent");
                }
                else if (((RegisterStep1)PageContent).commandExcuted.Equals("Cancel"))
                {
                    this.PageContent.RequestClose -= this.HandlePageClose;
                    _pageContent = new RegisterStartPage(_companyInfo, _registerInfo);
                    _pageContent.RequestClose += this.HandlePageClose;
                    OnPropertyChanged("PageContent");
                }
            }
            else if (this.PageContent is RegisterStep2)
            {
                this.OnRequestClose();
                this.Dispose();
            }
            else if (this.PageContent is RegisterStartPage)
            {
                if (((RegisterStartPage)PageContent).commandExcuted.Equals("Next"))
                {
                    _registerInfo = new RegisterInfo (((RegisterStartPage)PageContent).RegInfo);
                    this.PageContent.RequestClose -= this.HandlePageClose;
                    PageContent = new RegisterStep1(_registerPrinting, _companyInfo, _registerInfo);
                    this.PageContent.RequestClose += this.HandlePageClose;
                    OnPropertyChanged("PageContent");
                }
                else if (((RegisterStartPage)PageContent).commandExcuted.Equals("Cancel"))
                {
                    this.OnRequestClose();
                    this.Dispose();
                }
            }
        }

        #endregion

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Register;
                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }

        }
    }
}
